﻿package GameUtils
{	
	import com.junkbyte.console.Cc;
	import GameUtils.GameDataEvent;
	
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class XMLLoader extends EventDispatcher
	{
		//Events
		public const LIST_LOADED:String = 'LIST_LOADED';
		public const XML_LOADED:String  = 'XML_LOADED';
		public const ALL_XMLS_LOADED:String = 'ALL_XMLS_LOADED';
		//TMP
		private const GET_LIST_URL:String = 'http://62.212.69.47/php/getXMLs.php';
		private const XML_DIRECTORY_URL:String = 'http://62.212.69.47/XML/';
		//XMLs list loading
		private var FXMLSLinks:Vector.<String>;
		private var FList:XML;
		private var FListLoader:URLLoader;
		//For every XML in list						
		private var FXMLLoader:URLLoader;
		private var FXMLsLoadedCount:int;
		private var FXMLsTotalCount:int;
		private var FData:Vector.<TrackData>;
		
		public function XMLLoader() 
		{
			FXMLSLinks = new Vector.<String>;
			FListLoader = new URLLoader();
			FListLoader.addEventListener(IOErrorEvent.IO_ERROR, OnIOError);
			FListLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, OnSecurityError);
			FListLoader.addEventListener(Event.COMPLETE, OnListLoaded);
			FData = new Vector.<TrackData>;
			FXMLsLoadedCount = 0;
			FXMLsTotalCount = 0;
		}
		
		public function Load():void
		{			
			FListLoader.load(new URLRequest(GET_LIST_URL));			
		}
		
		private function OnIOError(e:IOErrorEvent):void
		{			
			Cc.error(e.text);
		}
		
		private function OnSecurityError(e:SecurityErrorEvent):void
		{
			Cc.error(e.text);
		}
		
		private function OnListLoaded(e:Event):void
		{
			FListLoader.removeEventListener(IOErrorEvent.IO_ERROR, OnIOError);
			FListLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, OnSecurityError);
			FListLoader.removeEventListener(Event.COMPLETE, OnListLoaded);
			
			FList = XML(e.target.data);
			var vList:XMLList = FList.descendants('file');
			for(var i in vList)
			{
				FXMLSLinks.push(XML_DIRECTORY_URL + vList[i].attribute('name'));
			}
			
			Cc.log('XML List loaded. XMLs on server: ' + FXMLSLinks.length);
			
			FXMLsTotalCount = FXMLSLinks.length - 1;			
			FXMLLoader = new URLLoader();
			FXMLLoader.addEventListener(Event.COMPLETE, OnXMLLoaded);
			FXMLLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, OnSecurityError);
			FXMLLoader.addEventListener(IOErrorEvent.IO_ERROR, OnIOError);
			
			LoadXML();
			
			var vEvent:GameDataEvent = new GameDataEvent(LIST_LOADED);
			vEvent.Data = vList;			
			dispatchEvent(vEvent);
		}
		
		private function LoadXML():void
		{			
			FXMLLoader.load(new URLRequest(FXMLSLinks[FXMLsLoadedCount]));
		}
		
		private function OnXMLLoaded(e:Event):void
		{
			//Parsing XML
			var vXML:XML = XML(e.target.data);
			var vData:TrackData = new TrackData();
			vData.File = vXML.Track.@File;
			vData.Name = vXML.Track.@Name;
			vData.Height = vXML.Track.@Height;
			vData.ID = FXMLsLoadedCount;			
			var vList:XMLList = vXML.descendants('Mark');
			var vMarkData:MarkData;
			for(var i in vList)
			{
				vMarkData = new MarkData();
				vMarkData.Index = vList[i].attribute('Index');
				vMarkData.y = vList[i].attribute('Y');
				vData.Data.push(vMarkData);					
			}
			
			if(vXML.Bots)
			{
				var vBots:XMLList = vXML.descendants('Bot');
				for(var j in vBots)
				{
					vData.Bots.push(vBots[j].@Name);
					trace('Detetced bot "' + vBots[j].@Name + '" on track ' + vData.Name + '');
				}
			}
			
			FData.push(vData);
			//Checking a count of parsed XMLs, if all of them are parsed - removing listener, dispatching complete event
			if(FXMLsLoadedCount < FXMLsTotalCount)
			{			
				FXMLsLoadedCount++;
				LoadXML();				
			}
			else
			{
				var vEvent:GameDataEvent = new GameDataEvent(ALL_XMLS_LOADED);
				vEvent.Data = FData;			
				dispatchEvent(vEvent);
				
				FXMLLoader.removeEventListener(Event.COMPLETE, OnXMLLoaded);
			}
		}
	}	
}
